<?php
/**
 * @author guoxiaoyong
 */
namespace console\controllers;
use yii\console\Controller;
use bc\wx\Auth;
use yii;
use bc\model\wx\User;
use bc\ar\MActiveRecord;

class UserController extends Controller
{
	
	
	/**
	 * 更新用户信息
	 */
	public function actionUpdate()
	{
		set_time_limit(0);
		$appid = Yii::$app->wechat->appid;
		$secret = Yii::$app->wechat->secret;
		
		$auth = new Auth($appid, $secret);
		$accessToken = $auth->accessToken;
		
		$wxUser = new \bc\wx\User();
		
		$user = new User();
		$user->sort = 'update_time DESC';
		//一天更新1000条
		$list = $user->getList(null, 0, 1000);
		foreach ($list as $row)
		{
			$info = $wxUser->getUserInfo($accessToken, $row['openid']);
			
			if($info)
			{
				$updateData['subscribe']      = $info['subscribe'];
				$updateData['nickname']       = $info['nickname'];
				$updateData['sex']            = $info['sex'];
				$updateData['language']       = $info['language'];
				$updateData['city']           = $info['city'];
				$updateData['province']       = $info['province'];
				$updateData['country']        = $info['country'];
				$updateData['headimgurl']     = $info['headimgurl'];
				$updateData['subscribe_time'] = $info['subscribe_time'];
				$updateData['unionid']        = $info['unionid'];
				$updateData['groupid']        = $info['groupid'];
				
				$user->modify($updateData, $row['id']);
			}
			
		}
		
		echo 'done';
	}
	
	
}

